package ciyuan.Player;

import ciyuan.Constants.PlayerDataType;
import ciyuan.DataManager.PlayerDataManager;
import ciyuan.Item.TypeInterface.WeaponType.*;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ArmorItem;
import net.minecraft.item.ItemStack;
import net.minecraft.util.Hand;

import java.util.concurrent.atomic.AtomicReference;

public class ComputedPlayerInfo {

    public static double getMeleeDmage(PlayerEntity player) {
        double originDamage = 1;
        ItemStack itemInMainHand = player.getItemInHand(Hand.MAIN_HAND);
        if (!itemInMainHand.isEmpty() && itemInMainHand.getItem() instanceof WeaponType) {
            WeaponType weaponType = (WeaponType) itemInMainHand.getItem();
            if (weaponType instanceof SwordType || weaponType instanceof BigSwordType) {
                originDamage += weaponType.getAttackDamage();
            }
        }
        originDamage += PlayerDataManager.getInt(player, PlayerDataType.STRENGTH) * 2;

        return originDamage;
    }

    public static double getRemoteDmage(PlayerEntity player) {
        double originDamage = 1;
        ItemStack itemInMainHand = player.getItemInHand(Hand.MAIN_HAND);
        if (!itemInMainHand.isEmpty() && itemInMainHand.getItem() instanceof WeaponType) {
            WeaponType weaponType = (WeaponType) itemInMainHand.getItem();
            if (weaponType instanceof BowType || weaponType instanceof CrossBowType) {
                originDamage += weaponType.getAttackDamage();
            }
        }
        originDamage += PlayerDataManager.getInt(player, PlayerDataType.AGILE) * 2;

        return originDamage;
    }

    public static double getMagicDamage(PlayerEntity player) {
        double originDamage = 1;
        ItemStack itemInMainHand = player.getItemInHand(Hand.MAIN_HAND);
        if (!itemInMainHand.isEmpty() && itemInMainHand.getItem() instanceof WeaponType) {
            WeaponType weaponType = (WeaponType) itemInMainHand.getItem();
            if (weaponType instanceof LawBookType || weaponType instanceof StaffType) {
                originDamage += weaponType.getAttackDamage();
            }
        }
        originDamage += PlayerDataManager.getInt(player, PlayerDataType.WISDOM) * 2;

        return originDamage;
    }

    public static double getDefence(PlayerEntity player) {
        AtomicReference<Double> defence = new AtomicReference<>(1.0);
        player.getArmorSlots().forEach(armor -> {
            if (armor.getItem() instanceof ArmorItem) {
                ArmorItem armorItem = (ArmorItem) armor.getItem();
                defence.updateAndGet(v -> v + armorItem.getDefense());
            }
        });

        defence.updateAndGet(v -> v + PlayerDataManager.getInt(player, PlayerDataType.STRONG) * 2);
        return defence.get();
    }

    public static long getMagicRegen(PlayerEntity player) {
        long originRegen = 1;
        originRegen += PlayerDataManager.getInt(player, PlayerDataType.WISDOM) * 2L;

        return originRegen;
    }

    public static double getCritChance(PlayerEntity player) {
        double originCritChance = 0;
        originCritChance += PlayerDataManager.getInt(player, PlayerDataType.ANALYSIS) * 2;

        return originCritChance;
    }

    public static double getCritDamage(PlayerEntity player) {
        double originCritDamage = 2;
        originCritDamage += PlayerDataManager.getInt(player, PlayerDataType.FLEXIBLE) * 0.05;

        return originCritDamage;
    }

    public static double getExpBonus(PlayerEntity player) {
        double originBonus = 1;

        return originBonus;
    }

    public static double getSearchAbility(PlayerEntity player) {
        double originAbility = 1;

        return originAbility;
    }

    public static long getMaxStamina(PlayerEntity player) {
        long originSt = 1000;
        originSt += PlayerDataManager.getInt(player, PlayerDataType.STRONG) * 50L;

        return originSt;
    }

}
